package dao

import (
	"context"
	"go-mall/model"
	"gorm.io/gorm"
)

type AddressDao struct {
	*gorm.DB
}

func NewAddressDao(ctx context.Context) *AddressDao {
	return &AddressDao{NewDBCliet(ctx)}
}

func NewAddressDaoByDB(db *gorm.DB) *AddressDao {
	return &AddressDao{db}
}

func (dao *AddressDao) CreateAddress(address *model.Address) error {
	return dao.DB.Model(&model.Address{}).Create(address).Error
}

func (dao *AddressDao) GetAddressByAid(aId string) (address *model.Address, err error) {
	err = dao.DB.Model(&model.Address{}).Where("id = ?", aId).Find(&address).Error
	return
}

func (dao *AddressDao) ListAddressByUserId(uid uint) (address []*model.Address, err error) {
	err = dao.DB.Model(&model.Address{}).Where("user_id = ?", uid).Find(&address).Error
	return
}

func (dao *AddressDao) UpdateAddress(aId uint, address *model.Address) error {
	return dao.DB.Model(&model.Address{}).Where("id=? AND user_id=?", address.ID, aId).Updates(&address).Error
}

func (dao *AddressDao) DeleteAddress(address *model.Address, id string) error {
	return dao.DB.Model(&model.Address{}).Where("id=?", id).Delete(&address).Error
}
